package test;

import static org.junit.Assert.*;

import java.sql.SQLException;

import library.*;
import library.controller.*;
import library.entity.*;
import org.junit.*;

public class SystemTestUser {
	LibrarianController librarianController;
	ReaderController readerController;
    Reader tmpReader;
    TestData testData;
	@Before
	public void setUp() throws SQLException{
		DaoUtil.init();
		testData = new TestData();
		librarianController = new LibrarianController();
        readerController = new ReaderController();
	}

	// Librarian Unit Testing

	@Test
	public void createLibrarian() throws SQLException {
		Librarian tmpLibrarian = new Librarian("newTmpLibrarian", "tmp");
		boolean result = librarianController.create(tmpLibrarian);
		assertTrue(result);
		librarianController.delete("newTmpLibrarian");
	}
	@Test
	public void selectLibrarian() throws Exception {
        Librarian librarian = librarianController.check("admin", "admin");
        assertEquals("admin", librarian.getUserName());
	}
	
	@Test
	public void selectLibrarianByWrongPassword() {
        assertNull(librarianController.check("testLibrarian", "test1"));
	}
	
	@Test
	public void deleteLibrarian() throws Exception {
		librarianController.create(new Librarian("tmpLibrarian", "tmp"));
		boolean result = librarianController.delete("tmpLibrarian");
		assertTrue(result);
	}

	@Test
	public void deleteLibrarianNotExist() throws Exception {
		assertFalse(librarianController.delete("tmpLibrarianNotExist"));
		assertEquals(ErrorCode.LIBRARIAN_NOT_FOUND,librarianController.getError().getErrorCode());
	}
	
    @Test
    /**
     *  Test Case: Try to get the Librarian that is not exist
     *  Expected Result: A User Not Find Exception should be throw
     * @throws Exception
     */
	public void notExistingLibrarian() {
        assertNull(librarianController.check("testLibrarian_abc1234", "testLibrarian_abc1234"));
	}	
	
	// Reader Unit Testing

    /**
     *  Test Case : select the reader: testReader
     *  Expected Result: A Reader Object with userName:  testReader
     * @throws Exception
     */
	@Test
	public void selectReader() throws Exception {
		Reader testReader = readerController.getReader("testReader");
		assertEquals("testReader", testReader.getUserName());
	}

    @Test
	public void selectReaderWithWrongCase() {
		Reader testReader = readerController.getReader("tEstReader");
		assertNull(testReader);
	}

    @Test
    /**
     *  Test Case: Try to delete the Reader that is not exist
     *  Expected Result: Reader not found error 
     * @throws Exception
     */
	public void deleteNotExistingReader() {
    	boolean result = readerController.delete("~~~notExistReader~~~");
    	assertFalse(result);
    	assertEquals(ErrorCode.READER_NOT_FOUND, readerController.getError().getErrorCode());
	}	
    
    @Test
    public void createReader(){
        tmpReader = new Reader("tmpReader", "Tmp Unit testing user","98765432","tmp");
        assertTrue(readerController.create(tmpReader));
    }
    
    @Test
    public void createRepeatReader(){
    	Reader existReader = testData.getTestReader();
        tmpReader = new Reader(existReader.getUserName(), "Tmp Unit testing user","98765432","tmp");
        assertFalse(readerController.create(tmpReader));
        assertEquals(ErrorCode.DUPLICATE_USER_FOUND, readerController.getError().getErrorCode());
    }
    
	@Test
	public void deleteReader() throws Exception {
		boolean result = readerController.delete("testReader");
		assertTrue(result);
	}
	
	@Test
	public void updateReader() throws Exception {
		Reader testReader = testData.getTestReader();
		testReader.setTel("91357913");
		boolean result = readerController.update(testReader);
		assertTrue(result);
	}

    @After
    public void cleanup() throws SQLException {
        if(tmpReader != null)
            readerController.delete(tmpReader.getUserName());
    }
}
